<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2006
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
* CORRECTBACKSLASH class
*
* Prior to v3.3, an extra backslash was written to database data - this corrects the problem.
*
*	$Header: /cvsroot/wikindx/wikindx3/core/init/CORRECTBACKSLASH.php,v 1.2 2006/04/28 04:46:46 sirfragalot Exp $
*
*****/
class CORRECTBACKSLASH
{
// Constructor
	function CORRECTBACKSLASH($vars = FALSE)
	{
		include_once("core/sql/SQL.php");
		$this->db = new SQL();
		$this->vars = $vars;
		$this->oldTime = time();
	}
// Prior to v3.3, all data was written to the database with extra slashes.  This routine corrects that error.
	function removeAllSlashes()
	{
		$this->db->escapeString = TRUE;
		$this->removeAllSlashes2('WKX_attachments', array('id', 'filename'));
		$this->removeAllSlashes2('WKX_bibtex_string', array('id', 'text'));
		$this->removeAllSlashes2('WKX_category', array('id', 'category'));
		$this->removeAllSlashes2('WKX_collection', array('id', 'collectionTitle', 
			'collectionTitleShort'));
		$this->removeAllSlashes2('WKX_config', array('title', 'contactEmail', 'description'));
		$this->removeAllSlashes2('WKX_creator', array('id', 'surname', 'firstname', 'initials', 'prefix'));
		$this->removeAllSlashes2('WKX_custom', array('id', 'custom_label'));
		$this->removeAllSlashes2('WKX_import_raw', array('id', 'text'));
		$this->removeAllSlashes2('WKX_keyword', array('id', 'keyword'));
		$this->removeAllSlashes2('WKX_news', array('id', 'title', 'news'));
		$this->removeAllSlashes2('WKX_papers', array('id', 'filename'));
		$this->removeAllSlashes2('WKX_publisher', array('id', 'publisherName', 'publisherLocation'));
		$this->removeAllSlashes2('WKX_resource', array('id', 'title', 'subtitle', 'url', 'nosort', 'isbn', 
			'field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8', 'field9'));
		$this->removeAllSlashes2('WKX_resource_abstract', array('id', 'abstract'));
		$this->removeAllSlashes2('WKX_resource_custom', array('id', 'custom_short', 'custom_long'));
		$this->removeAllSlashes2('WKX_resource_paraphrase', array('id', 'page_start', 'page_end', 
			'paragraph', 'chapter', 'section'));
		$this->removeAllSlashes2('WKX_resource_paraphrase_comment', array('id', 'comment'));
		$this->removeAllSlashes2('WKX_resource_paraphrase_text', array('id', 'text'));
		$this->removeAllSlashes2('WKX_resource_quote', array('id', 'page_start', 'page_end', 
			'paragraph', 'chapter', 'section'));
		$this->removeAllSlashes2('WKX_resource_quote_comment', array('id', 'comment'));
		$this->removeAllSlashes2('WKX_resource_quote_text', array('id', 'text'));
		$this->removeAllSlashes2('WKX_resource_musing', array('id', 'page_start', 'page_end', 
			'paragraph', 'chapter', 'section'));
		$this->removeAllSlashes2('WKX_resource_musing_text', array('id', 'text'));
		$this->removeAllSlashes2('WKX_resource_year', array('id', 'year1', 'year2', 'year3'));
		$this->removeAllSlashes2('WKX_tag', array('id', 'tag'));
		$this->removeAllSlashes2('WKX_user_bibliography', array('id', 'title', 'description'));
		$this->removeAllSlashes2('WKX_users', array('id', 'username', 'fullname', 'email'));
		$this->db->escapeString = FALSE;
		include_once("core/front/FRONT.php");
		$front = new FRONT($this->db);
		return $front->display();
	}
	function removeAllSlashes2($table, $array)
	{
		if($this->vars)
			$tablesDone = unserialize(base64_decode($this->vars['tablesDone']));
		else
			$tablesDone = array();
		if(array_search($table, $tablesDone) !== FALSE) // already upgraded this table.
			return;
		$recordset = $this->db->select(array($table), $array);
		while($row = $this->db->loopRecordSet($recordset))
		{
			$update = array();
			foreach($array as $element)
			{
				if(($element == 'id') || (!$row[$element]))
					continue;
				$update[$element] = stripslashes(stripslashes($row[$element]));
			}
			if(empty($update))
				continue;
			if(array_search('id', $array) !== FALSE)
				$condition = " WHERE " . $this->db->formatField('id') . '=' . $this->db->tidyInput($row['id']);
			else
				$condition = FALSE;
			$this->db->update($table, $update, $condition);
		}
		$tablesDone[] = $table;
// Check we have more than 4 seconds buffer before max_execution_time times out.
		if((time() - $this->oldTime) >= (ini_get("max_execution_time") - 4))
		{
			include_once("core/html/FORM.php");
			include_once("core/html/MISC.php");
			$pString = MISC::p("PHP's max_execution time is about to be exceeded (" . 
				ini_get("max_execution_time") . " seconds).");
			$pString .= MISC::p("Tables upgraded so far:");
			$pString .= MISC::p(join(", ", $tablesDone));
			$pString .= MISC::p("Hit continue to continue the database upgrade.");
			$pString .= FORM::formHeader("correctBackslashContinue");
			$pString .= FORM::hidden("tablesDone", base64_encode(serialize($tablesDone)));
			$pString .= MISC::p(FORM::formSubmit("Continue"));
			print $pString;
			die;
		}
	}
}
?>
